<?php
/*
 * Created on May 15, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php
/*
 * Created on May 15, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *  User_group Model Class
 *
 */
class  Provider_model extends CI_Model
{
	/**
	 * @var unknown_type
	 */
	var $id  ;  
	/**
	 * @var unknown_type
	 */
	var $name_vi  ; 
	/**
	 * @var unknown_type
	 */
	var $name_en  ; 
	/**
	 * @var unknown_type
	 */
	var $order_view  ;  

        function __construct() {
            parent::__construct();
        }

	function Provider_model()
	{
            parent::__construct();
	}
		
	function insert(){
				
		$sql = "INSERT INTO `gtb_provider` " .
				"(" .
				"`vi_name`, `en_name`, `order_view`)" .
				" VALUES ";
		
		$sql .= "(" .
				$this->db->escape($this->vi_name).", " .
				$this->db->escape($this->en_name).", " .
				intval($this->order_view).
				")";
			
		$this->db->query($sql);
		return $this->db->insert_id();
		
	}
	
	function update($provider){
		 $sql = "UPDATE gtb_provider 
				 SET ".
				" vi_name = " .  $this->db->escape($provider['vi_name']) . "," .
				" en_name = " .  $this->db->escape($provider['en_name']) . "," .
				" order_view = " .  intval($provider['order_view']) .
				" WHERE id_provider = " .intval($provider['id_provider']);
		$this->db->query($sql);
		return $this->db->affected_rows(); 
	}
	
	function delete($id){
		// Kiem tra trc khi xoa, neu co record tham chieu thi ko cho xoa.
		// .......... chua lam
		
		if(!is_array($id)) $id = array($id);
		$ids = implode(',', $id);
		$sql = "DELETE FROM gtb_provider WHERE id_provider IN ($ids)";
		//$sql = "DELETE FROM gtb_pcategory WHERE id_pcategory = " . intval($id);
		$this->db->query($sql);
		return $this->db->affected_rows(); 
	}
	
	function getAll(){
		$result = null;
		$sql = "SELECT s.* " .
				" FROM gtb_provider s " .
				" ORDER BY order_view ASC";
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		return $result;
	}
	
	function getAllCount(){
		$result = null;
		$sql = "SELECT COUNT(*) as num_record " .
				" FROM gtb_provider " ;
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		if($result) return $result[0]->num_record;
		
		return 0;
	}
		
	function getMaxPosition(){
		
		$sql = "SELECT MAX(order_view) as position_max
				FROM gtb_provider ";
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0]->position_max;
		}
       
		return 0;
	}
	
	function getById($provider_id){
		
		$sql = "SELECT *
				FROM gtb_provider 
				WHERE id_provider = " . intval($provider_id);
		$query = $this->db->query($sql);
		if($result = $query->result_array()){
			return $result[0];
		}
       
		return null;
	}
		
	function getAllAdmin($offset, $limit){
		
		$sql = "SELECT a.*,".
			 		" (SELECT MAX(t3.order_view) FROM gtb_provider t3) as max_order, 
					(SELECT MIN(t4.order_view) FROM gtb_provider t4) as min_order, 
					(SELECT (t5.id_provider) FROM gtb_provider t5 WHERE t5.order_view > a.order_view LIMIT 0,1) as id_next,
					(SELECT (t6.id_provider) FROM gtb_provider t6 WHERE t6.order_view < a.order_view ORDER BY t6.order_view DESC LIMIT 0,1) as id_preview " .
					" FROM gtb_provider a".
					" ORDER BY a.order_view ASC" .  
				 	" LIMIT " . intval($offset) . ", " . intval($limit);
		
		//echo $sql;
		$query = $this->db->query($sql);
		$result = $query->result_array();
                $query->free_result();
		return $result;
	}
	
	function getCountAllAdmin(){
		$result = null;
		
		$sql = "SELECT COUNT(*) as num_record 
				FROM gtb_provider ";
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	$query->free_result();
		return $result[0]->num_record;
	}

    function getAllByCategory($id_pcategory, $offset, $limit){

		$sql = "Select prov.* from gtb_product p
                inner join gtb_provider prov on p.id_provider = prov.id_provider ".
               " WHERE  p.id_pcategory = " . intval($id_pcategory).
                " group by p.id_provider" .
			   " LIMIT " . intval($offset) . ", " . intval($limit);

		//echo $sql;
		$query = $this->db->query($sql);
		$result = $query->result();
       	$query->free_result();
		return $result;
	}

	function getCountAllByCategory($id_pcategory){
		$result = null;

		$sql = "Select prov.id_provider from gtb_product p
                inner join gtb_provider prov on p.id_provider = prov.id_provider ".
               " WHERE  p.id_pcategory = " . intval($id_pcategory).
                " group by p.id_provider" ;

		$query = $this->db->query($sql);
		$result = $query->result();
       	$query->free_result();

        if($result) 
            return count($result);
            
		return 0;
	}
	
}

// EOF

